import json
import numpy as np
from scipy.signal import find_peaks
import matplotlib.pyplot as plt

# 加载 JSON 文件
with open('data.json', 'r') as file:
    data = json.load(file)

# 提取 data_points 和 scale_factor
data_points = data['data_points']['data']
scale_factor = data['data_points']['scale_factors'][0]['scale_factor']

# 将原始数据转换为实际值
actual_data = [point / scale_factor for point in data_points]

# 转换为 NumPy 数组以便于计算
actual_data_np = np.array(actual_data)

# 使用 find_peaks 函数检测尖峰
peaks, _ = find_peaks(actual_data_np, prominence=0.5)  # 根据需要调整prominence参数

# 打印检测到的尖峰索引及其对应的值
print("Detected peaks at indices:", peaks)
print("Peak values:", actual_data_np[peaks])

# 可视化结果
plt.figure(figsize=(10, 6))
plt.plot(actual_data_np, label='Data Points')
plt.plot(peaks, actual_data_np[peaks], 'x', color='red', label='Peaks')
plt.title('Data Points with Detected Peaks')
plt.xlabel('Index')
plt.ylabel('Value')
plt.legend()
plt.show()